<?php 

class GentityCommand extends CConsoleCommand
{

	/**
	 * Execute the action.
	 * @param array command line parameters specific for this command
	 */
	public function run($args)
	{
		
		$me = Config::Get('plugin.yii.modelEntityMap');
		$inh = array();
		
		$lsarrclass = new ReflectionClass('LsActiveRecord');
		foreach($me as $modelName => $entityShort){
			echo "model: $modelName; entity: $entityShort\r\n";
			
			$entityFull = LsClassInfo::GetEntityFullName($entityShort);
			
			$rclass = new ReflectionClass($modelName);
			preg_match(
				'#[\x5c/]+plugins[\x5c/]+([^\x5c/]+)[\x5c/]+#',
				$rclass->getFileName(),
				$tp
			);
			$plugin = $tp[1];
			
			
			$inherit = strtolower(LsClassInfo::GetClassInfo($entityFull, LsClassInfo::CI_PLUGIN, true)) != $plugin;
			
			$entityName = $inherit
				? $modelName
				: LsClassInfo::GetClassInfo($entityFull, LsClassInfo::CI_ENTITY, true)
			;
			
			$model = call_user_func(array($modelName, 'model'));
			//$relations = $model->getMetaData()->relations;
			$relations = $model->relations();
			$erclass = new ReflectionClass(/*class_exists($entityFull)*/$inherit ? $entityFull : 'Entity');
			$modelrclass = new ReflectionClass($modelName);

			ob_start();
			include dirname(__FILE__).'/gentity/entity_tpl.php';
			$c = ob_get_clean();
			
			$path = Yii::getPathOfAlias(
				'application.plugins.'.$plugin.'.classes.modules.'.$plugin.'.entity'
			).'/'.$entityName.'.entity.class.php';
			echo "path: $path\r\n";
			
			file_put_contents(
				$path,
				$c
			);
			
			if($inherit){
				$inh[$plugin][$entityFull] = 'Plugin'.ucfirst($plugin).'_Module'.ucfirst($plugin).'_Entity'.$entityName;
			}
			
			
		}
		
		echo "inherits:\r\n";
		foreach($inh as $plugin => $pinh){
			echo "plugin: $plugin\r\n";
			echo "'entity' => ";
			var_export($pinh);
		}
		
		
		if(@$args[0] == 'end'){
			exit;
		}
		
	}

}

?>